// mobile bind
// ============

define(['zepto', 'html2canvas', 'sui', 'zData', 'jrange', 'qrcode'], function($, html2canvas){
  return {
    init: function(data) {
      if(location.href.indexOf("#reloaded")==-1){
        location.href=location.href+"#reloaded";
        location.reload();
      }
      
      this.initRange();
      this.initCreateQRcode(data.primaryAgentId, data.rate);
      this.bindCreatePoster();
    },
    initRange: function() {
      $('.single-slider').jRange({
          from: 1,
          to: 10,
          step: 1,
          format: function(value, pointer) {
            return (Math.round(value) + "%");
          },
          width: 200,
          theme: 'theme-blue',
          showLabels: true,
          snap: true
      });
      
      $(document).on("click", ".iconfont-minus", function() {
    	var recruitVal = Number($(".single-slider").val()) < 1 ? '1' : Number($(".single-slider").val()) - 1  
        $('.single-slider').jRange('setValue', recruitVal.toFixed(2));
      })
      
      $(document).on("click", ".iconfont-plus", function() {
    	
    	var recruitVal = Number($(".single-slider").val()) > 10 ? '10' : Number($(".single-slider").val()) + 1  
    	$('.single-slider').jRange('setValue', recruitVal.toFixed(2));
      })

    },

    initCreateQRcode: function(primaryAgentId, rate){
    	
        var recruitVal = Number($('.commission-rate').val());
        var url = window.__CTX__.domain + "/login?primaryAgentId=" + primaryAgentId + '&rate='+ rate; // 二维码地址;
        var qrcode = new QRCode('qrcode', {
          text: url,
          width: 80,
          height: 80,
          colorDark : "#000",
          colorLight : "#fff",
          correctLevel : QRCode.CorrectLevel.L
        });
    },
    bindCreatePoster: function() {
          $.showPreloader('正在生成海报')
          var cntElem = document.querySelector('.poster-cover');
          var shareContent = cntElem;
          var width = shareContent.offsetWidth;
          var height = shareContent.offsetWidth * (16/9);
          var canvas = document.createElement("canvas");
          var scale = 2;
          canvas.width = width * scale; 
          canvas.height = height * scale;
          canvas.getContext("2d").scale(scale, scale);
          
          var rect = cntElem.getBoundingClientRect();
          canvas.getContext("2d").translate(-rect.left,-rect.top);
          
          setTimeout(function(){
            html2canvas(document.querySelector('.poster-cover'),{
              scale: scale,
              canvas: canvas,
              width: width,
              height: height,
              useCORS: true,
              x: 0,
              y: 0
            }).then(canvas => {
                $.hidePreloader()
                var imgUrl = canvas.toDataURL('image/jpg',0.92);
                $(".poster-cover").empty().append("<img src='"+ imgUrl +"' style='width: 100%;'>");
                $(".poster-cover").after("<p class='poster-success'>海报已生成，请长按图片保存到相册</p>");
            })
          },1000);
      }
  }
})